package com.fau.om.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.om.action.PublicAction;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalOrdersRidepinSta;
import com.fau.om.util.DateUtil;
import com.fau.om.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单搭销产品统计Action
 * @author kangyj
 * @datetime 2014年12月12日,上午2:16:26
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class RidePinProStaAction extends PublicAction implements
		ModelDriven<MedicalOrdersRidepinSta> {

	private static final long serialVersionUID = -979910119154410960L;

	private MedicalOrdersRidepinSta ridepinSta = new MedicalOrdersRidepinSta();
	
	/**
	 * @desc 订单搭销产品页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月12日,上午2:18:06
	 */
	@Action(value="orders_ride_pin_sta_init",results={@Result(name=SUCCESS,location="orders_ride_pin_sta_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 查询订单搭销产品数据
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月12日,上午2:18:34
	 */
	@Action(value="orders_ride_pin_sta_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getStatisticsManagerService().findridePinProSta(ridepinSta, admin, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出订单搭销产品统计
	 * @return null
	 * @author liy
	 * @datetime 2014年12月14日,下午11:22:09
	 */
	@Action(value = "orders_ride_pin_sta_expots")
	public String proExports() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单搭销产品统计信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsManagerService().ridePinStaExport(this.ridepinSta);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalOrdersRidepinSta getModel() {
		return ridepinSta;
	}

}
